package com.itheima.tcp3;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class ServerReader extends Thread {

    private Socket socket;

    public ServerReader(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        try {
            //3.从Socket管道中得到一个字节输入流
            InputStream is = socket.getInputStream();

            //4.把字节输入流包装成特殊数据输入流
            DataInputStream dis = new DataInputStream(is);

            while (true) {
                //5.读取客户端发送的数据
                String msg = dis.readUTF();
                System.out.println("服务端接收到的数据：" + "msg = " + msg);
                //客户端的IP和端口号
                System.out.println("客户端的IP地址：" + socket.getInetAddress().getHostAddress());
                System.out.println("客户端的端口号：" + socket.getPort());
                System.out.println("-----------------------------------------");
            }
        } catch (Exception e) {
            //e.printStackTrace();

            //下线
            System.out.println("1个客户端连接断开！"+socket.getInetAddress().getHostAddress());
        }


    }
}
